package client.game;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import client.*;
import net.miginfocom.swing.MigLayout;
import model.*;
import java.awt.*;

public class ClientGameFrame extends JFrame implements KeyListener,
		ActionListener {

	private JPanel contentPane;
	private TextArea textArea;
	private TextField textField;
	private Game game;
	private JPanel panel;
	private Label label_1, label;
	private TicTacToeButton button[] = new TicTacToeButton[9];
	private Button button_1;
	private String opponent;

	public ClientGameFrame(Game game, String opponent) {
		super("Chat Frame");
		this.game = game;
		this.opponent=opponent;
		label = new Label();
		// setBounds(100, 100, 500, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new MigLayout("", "[grow]", "[][grow][][][]"));

		contentPane.add(label, "flowx,cell 0 0,growx");

		panel = new JPanel();
		panel.setPreferredSize(new Dimension(300, 300));
		contentPane.add(panel, "cell 0 1,grow");
		panel.setLayout(new GridLayout(3, 3));
		for (int i = 0; i < 9; ++i) {
			button[i] = new TicTacToeButton(i);
			button[i].addActionListener(this);
			panel.add(button[i]);
		}

		label_1 = new Label("Chat with " + opponent);
		contentPane.add(label_1, "cell 0 2");

		textArea = new TextArea();
		textArea.setEditable(false);
		contentPane.add(textArea, "cell 0 3,grow");

		textField = new TextField();
		textField.setSize(new Dimension(0, 20));
		textField.setPreferredSize(new Dimension(0, 20));
		textField.addKeyListener(this);
		contentPane.add(textField, "cell 0 4,growx");

		button_1 = new Button("Restart");
		button_1.addActionListener(this);
		contentPane.add(button_1, "cell 0 0,alignx right");

		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		addWindowListener(new FrameHandler());
		setVisible(true);
		pack();
	}

	private class FrameHandler extends WindowAdapter {
		public void windowClosing(WindowEvent event) {
			setVisible(false);
			game.chatFrameClose();
		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		int keyCode = e.getKeyCode();
		if (keyCode == 10) {
			game.sendMessage(textField.getText());
			textField.setText("");
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == button_1)
			game.restart();
		else {
			TicTacToeButton button = (TicTacToeButton) e.getSource();
			if (button.getText().equals("") && game.canPress()) {
				game.buttonPressed(button);
			}
		}
	}

	public void setButton(ServerButton button) {
		int no = button.getNo();
		String symbol = button.getSymbol();
		this.button[no].setText(symbol);
	}

	public void showInfo(String info) {
		JOptionPane.showMessageDialog(null, info);
	}

	public void showMessage(Message message) {
		textArea.append(message.toString() + "\n");
	}

	public void userExit() {
		textArea.append("The other user closed the conversation!\n");
	}

	public void myTurn() {
		label.setText("Your turn!");
		label.setForeground(Color.GREEN);
		button_1.setEnabled(false);
	}

	public void opponentsTurn() {
		label.setText(opponent+"'s turn!");
		label.setForeground(Color.RED);
		button_1.setEnabled(false);
	}

	public void gameOver() {
		label.setText("Game over!");
		label.setForeground(Color.BLACK);
		button_1.setEnabled(true);
	}

	public void resetButtons() {
		for(int i=0;i<9;i++)
			button[i].setText("");
	}
}
